<?php

class Cellphonezone_Orderpreparation_Block_Adminhtml_Sales_Order_Grid extends MDN_AdvancedStock_Block_Adminhtml_Sales_Order_Grid
{ 
	protected $_aggregatedColumns       = null;
	public function __construct()
    {
        parent::__construct();
        $this->setCountTotals(true);
    }
    
 	/**
     * Retrieve collection class
     *
     * @return string
     */
    protected function _getCollectionClass()
    {
        return 'Orderpreparation/sales_order_grid_collection';
    } 
    
    /**
     * Manage mass actions
     * 
     * @return unknown
     */
    protected function _prepareMassaction()
    {
    	parent::_prepareMassaction();
    	if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/complete')) {
            $this->getMassactionBlock()->addItem('complete_order', array(
                 'label'=> Mage::helper('sales')->__('Complete'),
                 'url'  => $this->getUrl('Orderpreparation/CarrierTemplate/massComplete'),
            ));
        }    	        

        //raise event to allow other extension to add columns
        Mage::dispatchEvent('salesorder_grid_preparemassaction', array('grid'=>$this));
        
        return $this;
    }
    
	/**
     * Retrieve collection class
     *
     * @return string
     */
    /*protected function _getCollectionClass()
    {
        return 'Orderpreparation/sales_order_grid_collection';
    }*/
    
    protected function _prepareColumns()
    {    	    	
    	$this->addColumnAfter('shipment_state', array(
            'header'=>Mage::helper('Orderpreparation')->__('Shipment State'),
            'sortable'=>true,
            'index'=>'shipment_state',
            'width' => '80px',
            'type'  => 'text',
        ), 'grand_total');
        
        $this->addColumnAfter('customer_email', array(
            'header'=>Mage::helper('Orderpreparation')->__('Customer Email'),
            'sortable'=>true,
            'index'=>'customer_email',
        ), 'billing_name');        
        $this->addColumnAfter('base_grand_total_sum', array(
            'header' => Mage::helper('sales')->__('G.T. (Base)'),
            'index' => 'base_grand_total',
            'type'  => 'currency',
            'total' => 'sum',
            'currency' => 'base_currency_code',        
        ), 'customer_email');
        parent::_prepareColumns();
        $this->removeColumn('shipping_name');        
        $this->removeColumn('base_grand_total');        
        return $this;
    }
    
    private function removeColumn($columnId)
    {
    	if ($this->getColumn($columnId))
    	{
    		unset($this->_columns[$columnId]);
    	}
    }
    
    
	public function getCollection()
    {
        if (is_null($this->_collection)) {
            $this->setCollection(Mage::getModel($this->_getCollectionClass()));
        }
        return $this->_collection;
    }
    
	protected function _prepareCollection()
    {         
    	$collection = Mage::getResourceModel($this->_getCollectionClass());
    	$this->setCollection($collection);
        if ($this->getCountTotals()) {
            $totalsCollection = Mage::getResourceModel($this->_getCollectionClass())
                ->setAggregatedColumns($this->_getAggregatedColumns())
                ->isTotals(true);
            foreach ($totalsCollection as $item) {
                $this->setTotals($item);
                break;
            }
        }
        return parent::_prepareCollection();
    }
    
	

	protected function _getAggregatedColumns()
    {
        if (is_null($this->_aggregatedColumns)) {
        	$i = 1;
            foreach ($this->getColumns() as $column) {
                if (!is_array($this->_aggregatedColumns)) {
                    $this->_aggregatedColumns = array();
                }

                if ($column->hasTotal()) {
                    $this->_aggregatedColumns[$column->getIndex()] = $column->getTotal();
                }
                else 
                {
                	if ($i == 1)
                	{
                		$column->setTotalsLabel($this->__('Total:'));
                		$i++;
                	}
                	else
                	{
                		$column->setTotalsLabel('');
                	}
                }
            }
        }
        return $this->_aggregatedColumns;
    }
}

?>